﻿using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Text;
using TTEditor;
using TTEditor.Project;

namespace FakeUnity.Editor
{
    class WindowProject : IWindow
    {
        public WindowProject()
        {

        }
        ImFontPtr fontc2;

        public void OnClose()
        {
        }
        public bool candock => true;
        TTEditor.Project.Path pathnode;
        public void OnUpdate()
        {
            if (Editor.curProject != null && Editor.curProject.IsChange())
            {
                Editor.curProject.CheckResource();//只有先CheckResource 才會引發文件路徑不同
                pathnode = Editor.curProject.GetFiles();
            }
        }
        object __select;
        bool firstset = false;
        void GUI_FillTree(Path pathnode)
        {
            foreach (var d in pathnode.paths)
            {
                bool bSelect = __select == d;
                ImGuiTreeNodeFlags selectFlag = bSelect ? ImGuiTreeNodeFlags.Selected : ImGuiTreeNodeFlags.None;

                if (ImGui.TreeNodeEx(d.Name, selectFlag))
                {
                    GUI_FillTree(d);
                    ImGui.TreePop();
                }
                if (ImGui.IsItemClicked())//使用马后炮方式处理选择
                {
                    if (!bSelect && !firstset)//未选中状态的节点
                    {
                        __select = d;
                        Selector.Select(0, d);

                    }
                    firstset = true;
                }
            }
            foreach (var f in pathnode.files)
            {
                bool bSelect = __select == f;
                ImGuiTreeNodeFlags selectFlag = bSelect ? ImGuiTreeNodeFlags.Selected : ImGuiTreeNodeFlags.None;
                ImGui.TreeNodeEx(f.Name, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet | ImGuiTreeNodeFlags.NoTreePushOnOpen | selectFlag);
                if (ImGui.IsItemClicked())//使用马后炮方式处理选择
                {
                    if (!bSelect && !firstset)//未选中状态的节点
                    {
                        __select = f;
                        Selector.Select(0, f);
                    }
                    firstset = true;//IMGUI节点选中事件会一路往上，拦截一下

                }
            }
        }
        public void OnGUI()
        {
            unsafe
            {
                if (fontc2.NativePtr == null)
                {
                    var fonts = ImGui.GetIO().Fonts.Fonts;
                    for (var i = 0; i < fonts.Size; i++)
                    {
                        var font = fonts[i];
                        var glyph = font.FindGlyphNoFallback('中');
                        unsafe
                        {
                            if (glyph.NativePtr != null)
                            {
                                fontc2 = font;
                                var fontname = fontc2.GetDebugName();
                                break;
                            }
                        }
                    }
                }
            }
            if (Editor.curProject == null)
            {
                ImGui.Text("<No Project open>");
                return;
            }
            //该如何处理窗口空白处单击的事件呢？
            //if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
            //{
            //    __select = null;
            //    Selector.ClearSelect(0);
            //}
            firstset = false;
            GUI_FillTree(pathnode);
        }

        public void Show()
        {
        }
    }
}
